package cn.iocoder.yudao.module.cfs.controller.admin.company;

import cn.iocoder.yudao.module.cfs.controller.admin.company.vo.SysCompanyPageReqVO;
import cn.iocoder.yudao.module.cfs.controller.admin.company.vo.SysCompanyRespVO;
import cn.iocoder.yudao.module.cfs.controller.admin.company.vo.SysCompanySaveReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.cfs.dal.dataobject.company.SysCompanyDO;
import cn.iocoder.yudao.module.cfs.service.company.SysCompanyService;

@Tag(name = "管理后台 - sys_company")
@RestController
@RequestMapping("/cfs/sys-company")
@Validated
public class SysCompanyController {

    @Resource
    private SysCompanyService sysCompanyService;

    @PostMapping("/create")
    @Operation(summary = "创建sys_company")
    @PreAuthorize("@ss.hasPermission('cfs:sys-company:create')")
    public CommonResult<Long> createSysCompany(@Valid @RequestBody SysCompanySaveReqVO createReqVO) {
        return success(sysCompanyService.createSysCompany(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新sys_company")
    @PreAuthorize("@ss.hasPermission('cfs:sys-company:update')")
    public CommonResult<Boolean> updateSysCompany(@Valid @RequestBody SysCompanySaveReqVO updateReqVO) {
        sysCompanyService.updateSysCompany(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除sys_company")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cfs:sys-company:delete')")
    public CommonResult<Boolean> deleteSysCompany(@RequestParam("id") Long id) {
        sysCompanyService.deleteSysCompany(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除sys_company")
                @PreAuthorize("@ss.hasPermission('cfs:sys-company:delete')")
    public CommonResult<Boolean> deleteSysCompanyList(@RequestParam("ids") List<Long> ids) {
        sysCompanyService.deleteSysCompanyListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得sys_company")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cfs:sys-company:query')")
    public CommonResult<SysCompanyRespVO> getSysCompany(@RequestParam("id") Long id) {
        SysCompanyDO sysCompany = sysCompanyService.getSysCompany(id);
        return success(BeanUtils.toBean(sysCompany, SysCompanyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得sys_company分页")
    @PreAuthorize("@ss.hasPermission('cfs:sys-company:query')")
    public CommonResult<PageResult<SysCompanyRespVO>> getSysCompanyPage(@Valid SysCompanyPageReqVO pageReqVO) {
        PageResult<SysCompanyDO> pageResult = sysCompanyService.getSysCompanyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SysCompanyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出sys_company Excel")
    @PreAuthorize("@ss.hasPermission('cfs:sys-company:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSysCompanyExcel(@Valid SysCompanyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SysCompanyDO> list = sysCompanyService.getSysCompanyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "sys_company.xls", "数据", SysCompanyRespVO.class,
                        BeanUtils.toBean(list, SysCompanyRespVO.class));
    }

}